const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scimage:[],
    posterurl:[],
    hidden:false,
    xlselects:[],
    inpclicknum:0,
    inpvalue:"请选择",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    app.comrequest('https://live.ipass.cc/myapi2/livephoto/lpapi.php',{num:5}).then(res=>{
      that.setData({
        xlselects:res
      })
    })

  },
  //点击预览
  fimgclick(){
    var that=this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success (res) {
        if(res.tapIndex==0){
          that.chooseWxImage("album")
        }else if(res.tapIndex==1){
          that.chooseWxImage("camera")
        }
      }
    })
  },
  //点击打开相册，拍照
  chooseWxImage(type){
    var that=this
    wx.chooseImage({
      count: 9,
      sizeType: 'original',
      sourceType: [type],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          scimage:res.tempFilePaths
        })
        const tempFilePaths = res.tempFilePaths
        var templenght=tempFilePaths.length
        wx.showLoading({
          title: '加载中',
          mask: true,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
        for(var i=0;i<tempFilePaths.length;i++){
          console.log(tempFilePaths[i])
          wx.uploadFile({
          filePath: tempFilePaths[i],
          name: 'file',
          url: 'https://live.ipass.cc/myapi2/livephoto/glyupload.php',
          success(res){
            if(res.data=="非合法的文件格式"){
              wx.showToast({
                title: "图片请小于2MB",
                icon:'none',
                duration:1000
              })
            }else{
              var posterurl=that.data.posterurl;
              posterurl.push(res.data)
              that.setData({
                posterurl
              })
              var poslrnght=posterurl.length
              if(templenght==poslrnght){
                wx.hideLoading()
              }
              console.log(that.data.posterurl)
            }
          }
        })
        }
      }
    })
  },
  //图片上传到服务器
  sclick(){
    var that=this
    var posterurls=that.data.posterurl
    var inpvalue=that.data.inpvalue
    if(inpvalue=="请选择"||posterurls.length==0){
      wx.showToast({
        title: '错误：请检查是否上传预览或活动类型是否选择',
        icon:'none'
      })
    }else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title:'提示',
        content:'确定将图片上传到服务器吗？',
        success:res=>{
          if(res.confirm==true){
            //发送请求
            for(var i=0;i<posterurls.length;i++){
              var pitem=posterurls[i]
              app.comrequest('https://live.ipass.cc/myapi2/livephoto/lpapi.php',{num:2,posterurl:pitem,inpvalue:inpvalue}).then(res=>{
              console.log(res)
             })
            }
          }
        }
      })
      that.setData({
        posterurl:[]
      })
    }
  },
  inpclick(){
    var inpclicknum=this.data.inpclicknum
    inpclicknum=inpclicknum+1
    this.setData({
        inpclicknum
    })
    if(inpclicknum%2==0){
        this.setData({
            hidden:false,
        })
    }else{
        this.setData({
            hidden:true,
        })
    }  
  },
  xlclick(e){
    this.setData({
        inpvalue:e.currentTarget.dataset.value,
        hidden:false,
        inpclicknum:this.data.inpclicknum+1
    })
    var inpvalue=e.currentTarget.dataset.value;
    
 }
})